//成品
var app_url = location.origin;
 var MANAGE_MODE=false;
 var TEAM=0;
 var PlaceOrder=false;//代下单
 var RATE=Number($.cookie('RingRate'));//汇率
var indexCount=1;
var ends=true;
var loads=true;
(function () {
// alert("111")
    var inlays = {
        init: function () {
        	args = GetUrlParms();
            //localStorage.removeItem('das');
			if(args["team"]!=undefined){						
				 TEAM = args["team"];//产品ID					
			} 
			if(args["cid"]!=undefined){						
				 Customer_id = args["cid"];	
				 PlaceOrder=true;//代下单
			}

            this.bindEvents();
			//筛选条件
             loadInlays();

        },
        bindEvents: function () { 
            var self = this;
             //筛选
            $("#term_app_btn").on("click",function(){
            	if(PlaceOrder==true){
            		location.href="/Customer/RingFilter?team=3&cid="+Customer_id+'&userName='+NullFormat(getUrlParam('userName'));
            	}else{
            		location.href="/Customer/RingFilter?team="+TEAM;
            	}
            });


            //点击进入产品详情
           	$("#inlays").on("click","#app_link_item",function(e){
                var das={
                    productno :$(this).parent().attr('data-productno'),
                    productsupplier :$(this).parent().attr('data-productsupplier')
                };
           		var link_url=$(this).attr("data-src");
           		if(APP){
           			var appJson={};
           			appJson.title="成品详情";
           			appJson.url=app_url+link_url;	
           			YiYi_APP.showNewPage(JSON.stringify(appJson));
           		}else{
                    localStorage.setItem('das',JSON.stringify(das));
           			location.href=link_url;
           		}        		
           });
            //点击菜单
			$("#show_menu_btn").on("click",function(){
				$("#assistant_menu_box").toggle();
			});
			//关闭菜单
			$("#assistant_menu_box").on("click",function(){
				$("#assistant_menu_box").hide();
			});
           //新增
			$("#add_btn").on("click",function(){
				var link_url="/Team/RingManagement?type=3";
				if(APP){
					var appJson={};
           			appJson.title="成品新增";
           			appJson.url=app_url+link_url;	
           			YiYi_APP.showNewPage(JSON.stringify(appJson));
				}else{
					location.href=link_url;
				}
			});

        },


    };
    inlays.init();
    
})();

//加载数据
 function loadInlays(pageIndex,pageSize) {
     indexCount++;
     pageIndex=pageIndex?pageIndex:1;
    var self = this;
     if(pageIndex == 1){
         myApp.showIndicator();//显示加载中
     }
     loads=false;
    if(APP){
        $(".footer_box").hide();
    }
    var params = PARAMS;//APP手机过来的参数
    if(params==null){
        params={};
    }
    if(TEAM==2){
        params.IsMyStock=true;//现货
    }else{
        params.OnShelves = 1;
    }
    if(TEAM==0){
        params.CustomerID = CustomerID;//客户ID
    }
    if(TEAM==3){
        params.UserID=UserID;
    }
    params.useDict = true;
     params.pageSize = pageSize?pageSize:50;
     params.pageIndex = pageIndex;
    params.GoodsType=3;
    var params = JSON.stringify(params);
    // var params = JSON.stringify(params);
    var url ="/Team/GetProductData";
    $.ajax({
        url: url,
        data: params,
        type: "POST",
        contentType: "application/json; charset=utf8",
        timeout: 30000, //超时时间：30秒
        success: function (result) {
            clearProductBtn(); //清空选项
            tempSupplier = 0;
            var res=eval('(' + result + ')')
            if (res.msgcode == true) {
                window.GOODS_ITEMS = res.msginfo.rows;
                var dtRenderStart = new Date().getTime();
                isSearching = false;
                if (res.msginfo.rows.length == 0) {

                    if(JSON.parse(params).pageIndex!=1){
                        ends=false;
                        $('#endload').text('已全部加载完毕');
                    }else {
                        ends=true;
                        $('#endload').text('')
                        $("#inlays").html('<div class="no_data_tip" style="color:red">没有符合条件的结果</div>');
                    }

                } else {
                    if(res.msginfo.rows.length>0&&res.msginfo.rows.length<50){
                        ends=false;
                        $('#endload').text('已全部加载完毕')
                    }
                        var inlays = [];
                    for (var i = 0; i < res.msginfo.rows.length; i++) {
                        var row = res.msginfo.rows[i];

                        inlays.push(
                            '<li data="', row.Supplier, '" itemtype="" itemid="', row.ID, '"  data-ProductNo="',row.OriginSN,'"  data-ProductSupplier="',row.Supplier,'" class="',i%2==0?"":"mark",'">'
                        )
                        if(TEAM ==1 || TEAM ==2){
                            inlays.push(
                                '<a href="#" data-src=', "/", "Team/RingDetails/?ID=", row.ID, "&team=", TEAM,PlaceOrder?"&cid="+Customer_id:"","&styleNo=", row.StyleNo,' id="app_link_item" data-i="' + row.Category + '" data-type="' + GetCategory("inlays", row.Category) + '">'
                            )
                        }else if(TEAM==3){
                            inlays.push(
                                '<a href="#" data-src=', "/", "Team/RingDetails/?ID=", row.ID, "&team=", TEAM,PlaceOrder?"&cid="+Customer_id:"", "&userName=",getUrlParam('userName'),"&styleNo=",row.StyleNo,' id="app_link_item" data-i="' + row.Category + '" data-type="' + GetCategory("inlays", row.Category) + '">'
                            )
                        }else{
                            inlays.push(
                                '<a href="#" data-src=', "/", "Customer/RingDetails/?ID=", row.ID, "&team=", TEAM,"&typeNo=", 1,"&styleNo=",row.StyleNo,PlaceOrder?"&cid="+Customer_id:"",' id="app_link_item" data-i="' + row.Category + '" data-type="' + GetCategory("inlays", row.Category) + '">'
                            )
                        }
                        inlays.push(
                            '<div class="image_app_box">',
                            '<img style="" class="product_img lazy" data-src="',imgSrc(row.Images),'" src="'+imgSrc(row.Images)+'" alt="成品" />',
                            '<div class="stock_img_box">',isStockImg(row.IsStock),'</div>',
                            '</div>'
                        )


                        if(TEAM==0){
                            inlays.push(
                                '<div class="product_data">',
                                '<h4>'
                            );
                            if(RMBRingPrice_td_new==true){
                                inlays.push(
                                    '<span style="font-size:0.6rem" class="CurrentRMB">￥<b style="font-size:0.6rem" title="价格">'+formatRMBPrice(row.Price)+'</b></span>'
                                );
                            }
                            if(priceRing_td_new==true){
                                inlays.push(
                                    '<span style="font-size:0.6rem" class="CurrentUSD">$<b style="font-size:0.6rem" title="美金价格">'+formatUSDPrice(row.Price/RATE)+'</b></span>'
                                );
                            }

                            inlays.push(
                                '</h4><div class="data_box">'
                            );

                            inlays.push(
                                '<div>'+
                                '<b title="系列名称">'+row.Series+'</b>'+
                                '<b style="float:right" title="所在地">'+GetText("inlays",row.Category,row.Country,"Country")+row.State+'</b>'+
                                '</div>'
                            )

                            inlays.push(
                                '<div class="product_div_bg"><b title="总重量">'+row.Weight+'</b>'
                            )
                            inlays.push(
                                '<b title="材质">'+ GetText("inlays", row.Category, row.Material, "Material")+ '</b>'
                            )
                            inlays.push(
                                '<b title="形状">'+ GetText("inlays",row.Category,row.MainShape,"MainShape")+ '</b>'
                            )
                            inlays.push(
                                '<b title="尺寸">'+ row.Measurement+ '</b></div>'
                            )

                        }else if(TEAM==1 || TEAM==3 ){
                            inlays.push(
                                '<div class="product_data">',
                                '<h4>'
                            );

                            inlays.push(
                                (menu_ordering_rings_ConfirmPrice || RoleID ==1)?'<span style="font-size:0.6rem" class="CurrentRMB">￥<b style="font-size:0.6rem" title="价格">'+formatRMBPrice(row.Price)+'</b></span>':''
                            );

                            inlays.push(
                                (menu_ordering_rings_ConfirmUSD || RoleID ==1)?'<span style="font-size:0.6rem" class="CurrentUSD">$<b style="font-size:0.6rem" title="美金价格">'+formatUSDPrice(row.Price/RATE)+'</b></span>':''
                            );

                            inlays.push(
                                '</h4><div class="data_box">'
                            );

                            inlays.push(
                                '<div class="series"><b style="display:inline" title="系列名称">'+row.Series+'</b>' +
                                '<b style="float:right" title="所在地">'+GetText("inlays",row.Category,row.Country,"Country")+row.State+'</b>' +
                                '</div>'
                            )

                            inlays.push(
                                '<div class="product_div_bg">'
                            )

                            inlays.push(
                                (menu_ordering_rings_Weight || RoleID ==1)?'<b title="总重量">'+row.Weight+'</b>':''
                            )
                            inlays.push(
                                (menu_ordering_rings_Material || RoleID == 1)?'<b title="材质">'+ GetText("inlays", row.Category, row.Material, "Material")+ '</b>':''
                            )
                            inlays.push(
                                (menu_ordering_rings_MainShape || RoleID == 1)?'<b title="形状">'+ GetText("inlays",row.Category,row.MainShape,"MainShape")+ '</b>':''
                            )
                            inlays.push(
                                '<b title="尺寸">'+ row.Measurement+ '</b></div>'
                            )

                        }else if(TEAM==2){
                            inlays.push(
                                '<div class="product_data">',
                                '<h4>',
                                returnPrice(row),
                                '<span style="font-size:0.6rem" class="CurrentUSD">$<b title="美金价格" style="font-size:0.6rem">'+formatUSDPrice(row.Price/RATE)+'</b></span>',
                                '</h4>',
                                '<div class="data_box">',
                                '<div class="series"><b style="display:inline" title="系列名称">'+row.Series+'</b>' +
                                '<b style="float:right" title="所在地">'+GetText("inlays",row.Category,row.Country,"Country")+row.State+'</b>' +
                                '</div>'+
                                '<div class="product_div_bg"><b title="总重量">'+row.Weight+'</b>'+
                                '<b title="材质">'+ GetText("inlays", row.Category, row.Material, "Material")+ '</b>'+
                                '<b title="形状">'+ GetText("inlays",row.Category,row.MainShape,"MainShape")+ '</b>'+
                                '<b title="尺寸">'+ row.Measurement+ '</b></div>'
                            );
                        }

                        inlays.push(
                            '<div class="product_div_bg"><b title="主石重量">'+row.MainWeight+'</b>'
                        )
                        inlays.push(
                            '<b title="颜色">' + GetText("inlays", row.Category, row.MainColor, "MainColor") + '</b>'
                        );
                        if(row.Category == 2 || row.Category ==3){
                            inlays.push(
                                '<b title="净度">' +  row.MainClarity + '</b>'
                            );
                        }

                        if(row.Category == 4 || row.Category ==5 || row.Category == 6 || row.Category ==7 || row.Category ==8){
                            inlays.push(
                                '<b title="产地">' +  GetText("inlays",row.Category,row.Place,"Place") + '</b>'
                            );
                            inlays.push(
                                '<b title="处理" class="process">' +  GetText("inlays",row.Category,row.Process,"Process") + '</b>'
                            );
                        }
                        inlays.push(
                            '</div>'
                        )
                        inlays.push(
                            '</div></div></a>' +
                            '<div class="check_list_box"><span class="checkbox" lang="'+row.ID+'" data-receiptstatus="'+row.ReceiptStatus+'" ' +
                            'data-rmb="'+formatRMBPrice(row.PurchaseRMB)+'" data-usd="'+formatUSDPrice(row.PurchaseUSD)+'" data-styleNo="'+row.StyleNo+'" ' +
                            'data-style="'+row.Style+'" data-series="'+row.Series+'" data-certType="'+row.CertType+'" data-Tag="'+row.Tag+'"></span></div>' +
                            '</li>'
                        )

                    }

                    $("#inlays").append(inlays.join(''));

                    //懒加载
                    var n = 0,
                        imgNum = $("#inlays li").length,
                        img = $('#inlays li .product_img');
                    // myApp.initImagesLazyLoad(img)
                    $$('div.lazy').trigger('lazy');
                    //图片加载出来时居中显示 放在 framework.js

                    $(".image_app_box .product_img").load(function(){
                        //图片加载出来时居中显示  产品图 成品
                        var w=$(this).width();
                        var h=$(this).height();
                        var docu_w=$(window).width();
                        if(docu_w>760){
                            var screenW=244;
                        }else{
                            var screenW=docu_w/2;
                        }
                        if(w>h){
                            var w_scale=w/screenW;
                            var now_h=h/w_scale;
                            var pa_top=screenW/2-now_h/2;
                            $(this).css({
                                "width":"100%",
                                "padding-top":pa_top+"px",
                                "padding-bottom":pa_top+"px"
                            });
                        }else if(h>w){
                            var h_scale=h/screenW;
                            var now_w=w/h_scale;
                            var pa_left=screenW/2-now_w/2;
                            $(this).css({
                                "height":"100%",
                                "padding-left":pa_left+"px",
                                "padding-right":pa_left+"px"
                            });
                        }else{
                            $(this).css({
                                "height":"100%",
                                "width":"100%"
                            });
                        }

                    });


                }
                currentShow();  //币种设置
                console.info(new Date().getTime() - dtRenderStart + "ms");
                loads=true;
            } else {
                myApp.alert(res.msginfo);
            }
            myApp.hideIndicator();//隐藏加载中

        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            myApp.hideIndicator();//隐藏加载中
            var isLogin=getcookie("CustomerID");
            if(isLogin==null || isLogin=="(null)" ||!isLogin || isLogin=='nil'){
                myApp.alert("未搜索到相关数据");
            }else{
                myApp.alert("网络超时,请重试");

            }
        },
    });
}
//没权限返回的价格
function returnPrice(data){
	//if(data.Price || data.Price==0){
		return '<span style="font-size:0.6rem" class="CurrentRMB">￥<b title="价格" style="font-size:0.6rem">'+formatRMBPrice(data.Price)+'</b></span>';
	//}else{
	//	return '<span style="font-size:14px;">￥<b title="价格">权限不足</b></span>';	
	//}
}

//显示现货或者定制的图片
function isStockImg(val){
	if(val==1){//现货
		return '<img src="../images/Coods_in_stock.svg" alt=""/>'
	}
	if(val==2){//定制
		return '<img src="../images/Customized.svg" alt=""/>'
	}
}
//优先显示简称
function ConvertSupplier(data) {
//	if(data.Supplier != undefined) {
//		if(data.ShortName != "") {
//			return data.ShortName;
//		} else {
			return data.Supplier;
//		}
//	} else
//		return "-";
}
//显示颜色
function isShow(color,main,total,type){
	if(type != 1){
			return [
			'<b title="总重量">'+total+'</b>',
			'<b title="主石重量">'+main+'</b>',
			'<b title="颜色">' + GetText("inlays", type, color, "MainColor") + '</b>',
			].join('')
	}else{
			return [
			'<b title="总重量">'+total+'</b>',
			//'<b title="'+_("颜色")+'">'+val+'</b>',			
			].join('')
	}
}

//权限设置
function isShowPower(color,main,total,type){
    if(type != 1){
        return [
            '<b title="总重量">'+total+'</b>',
            '<b title="主石重量">'+main+'</b>',
            '<b title="颜色">' + GetText("inlays", type, color, "MainColor") + '</b>',
        ].join('')
    }else{
        return [
            '<b title="总重量">'+total+'</b>',
        ].join('')
    }
}


//返回图片链接
function imgSrc(images) {
    if (images) {
        images = images.split('|');
        var small = images[0];
        var big = small.replace('400x400', '1920x1920');
        return small;
    }
    return '../images/no_img.jpg';//无图时返回默认图片
}




